package it.ap.sa.acqua.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import it.ap.sa.acqua.dao.model.TUser;
import it.ap.sa.acqua.dao.model.TUserProfile;
import it.ap.sa.core.dao.util.AbstractGenericEntityDao;
/**
 * @author Alessandro Paolillo
 */
@Repository
public class TUserProfileDao  extends AbstractGenericEntityDao<TUser>{

	/**
	 * logger
	 */
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	  public TUserProfile findById(int id) {
		  try{
				String  hql     = "from " + TUserProfile.class.getName() + " where id = :id";
				Session session = this.getSessionFactory().getCurrentSession();
				Query   query   = session.createQuery(hql);

				query.setLong("id", id);

				return (TUserProfile)query.uniqueResult();
			}catch(Exception e){
				if(logger.isDebugEnabled())
					logger.debug("Error in findById", e);
				throw e;
			}
	    }
	 
	    public TUserProfile findByType(String type) {

	        try{
				String  hql     = "from " + TUserProfile.class.getName() + " where type = :type";
				Session session = this.getSessionFactory().getCurrentSession();
				Query   query   = session.createQuery(hql);

				query.setString("type", type);

				return (TUserProfile)query.uniqueResult();
			}catch(Exception e){
				if(logger.isDebugEnabled())
					logger.debug("Error in findByType", e);
				throw e;
			}
	    }
	     
	    @SuppressWarnings("unchecked")
	    public List<TUserProfile> findAll(){
	        try{
				String  hql     = "from " + TUserProfile.class.getName();
				Session session = this.getSessionFactory().getCurrentSession();
				Query   query   = session.createQuery(hql);

				return (List<TUserProfile>)query.list();
			}catch(Exception e){
				if(logger.isDebugEnabled())
					logger.debug("Error in findAll", e);
				throw e;
			}
	    }
}